package com.xinsoft.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinsoft.entity.dto.SysTruckPageParam;
import com.xinsoft.entity.po.SysTruck;
import com.xinsoft.mapper.SysTruckMapper;
import com.xinsoft.service.SysTruckService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author xuhb
* @description 针对表【sys_truck(车辆管理)】的数据库操作Service实现
* @createDate 2023-05-19 13:40:38
*/
@Service
public class SysTruckServiceImpl extends ServiceImpl<SysTruckMapper, SysTruck>
    implements SysTruckService{

    @Resource
    SysTruckMapper sysTruckMapper;
    @Override
    public IPage<SysTruck> listPageTruck(SysTruckPageParam param) {
        //分页
        Page<SysTruck> page = new Page<SysTruck>(param.getCurrentPage(), param.getPageSize());
        LambdaQueryWrapper<SysTruck> queryWrapper = new LambdaQueryWrapper<>();
        //查询条件
        queryWrapper
                .like(StringUtils.isNotEmpty(param.getTruckDriver()),SysTruck::getTruckDriver,param.getTruckDriver())
                .like(StringUtils.isNotEmpty(param.getTruckNo()),SysTruck::getTruckNo,param.getTruckNo())
                .like(StringUtils.isNotEmpty(param.getPhoneNo()),SysTruck::getPhoneNo,param.getPhoneNo())
                .like(param.getTruckColor()!=null,SysTruck::getTruckColor,param.getTruckColor());
        IPage<SysTruck> resultPage = sysTruckMapper.selectPage(page, queryWrapper);

        //返回值
        return resultPage;
    }

    @Override
    public String saveTruck(SysTruck truck) {
        if(truck.getPhoneNo()!=null&&truck.getPhoneNo().length()!=11){
            return "手机号码格式不对";
        }
        LambdaQueryWrapper<SysTruck> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(truck.getTruckNo()),SysTruck::getTruckNo,truck.getTruckNo());
        List<SysTruck> truckList = sysTruckMapper.selectList(queryWrapper);
        if(truckList!=null&&truckList.size()>0){
            return "重复车牌号不允许添加";
        }else{
            this.save(truck);
            return "";
        }

    }

    @Override
    public String updateTruck(SysTruck truck) {
        if(truck.getPhoneNo()!=null&&truck.getPhoneNo().length()!=11){
            return "手机号码格式不对";
        }
        LambdaQueryWrapper<SysTruck> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(truck.getTruckNo()),SysTruck::getTruckNo,truck.getTruckNo())
                .ne(truck.getId()!=null,SysTruck::getId,truck.getId());
        List<SysTruck> truckList = sysTruckMapper.selectList(queryWrapper);
        if(truckList!=null&&truckList.size()>0){
            return "重复车牌号不允许添加";
        }else{
            this.updateById(truck);
            return "";
        }
    }


}




